package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IUserInfoDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Activity;
import com.qiyou.entity.ActivityInvitation;
import com.qiyou.entity.Blog;
import com.qiyou.entity.PlaceAttestation;
import com.qiyou.entity.ScenicSpot;
import com.qiyou.entity.UserInfo;

/**
 * UserInfoServlet 加载用户信息
 */
@WebServlet("/UserInfo")
public class UserInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    request.setCharacterEncoding("utf-8");
	    response.setContentType("text/html;charset=UTF-8");
	    
	    IUserInfoDao userInfoDao = DaoFactory.getUserInfoDao();
	
	    String userID = request.getParameter("id");
	    int modify = -1;
	    int iact = 0;
	    int iactiv = 0;
	    int iblog = 0;
	    int iatt = 0;
	    try {
	        modify = Integer.parseInt(request.getParameter("modify"));
        } catch (Exception e) {
        }
	    if (modify <= 0) {
            UserInfo userInfo = null;
            
            try {
                userInfo = userInfoDao.getUserInfo(userID);
                
                List<Activity> ac = DaoFactory.getActivityDao().getActivities(userID);
                List<ActivityInvitation> aciv = DaoFactory.getIActivityInvitationDao().getInvitations(userID);
                List<Blog> blogs = DaoFactory.getBlogDao().getBlogs(userID);
                List<ScenicSpot> pat = DaoFactory.getPlaceAttestationDao().getPlaceAttestations(userID);

                iact = ac == null ? 0: ac.size();
                iactiv = aciv == null ? 0: aciv.size();
                iblog = blogs == null ? 0: blogs.size();
                iatt = pat == null ? 0: pat.size();
                
            } catch (SQLException e) {
//                e.printStackTrace();
            }
            
            if (null != userInfo) {
                request.setAttribute("userinfo", userInfo);
                
                request.setAttribute("iact", iact);
                request.setAttribute("iactiv", iactiv);
                request.setAttribute("iblog", iblog);
                request.setAttribute("iatt", iatt);
                
                switch (modify) {
				case 0:
					request.getRequestDispatcher("userinfoedit.jsp").forward(request, response);
					break;
				default:
					request.getRequestDispatcher("userinfo.jsp").forward(request, response);
					break;
				}
            }else {
                response.getWriter().print("<script  language='javascript'>alert('请求的用户信息有误');history.go(-1);</script>");
            }
        }else {
            this.updateUserInfo(userID, modify, request, response);
        }
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private void updateUserInfo(String userID,int modify,HttpServletRequest request, HttpServletResponse response){
	    if (modify <= 0) {
            return;
        }
	    
	    UserInfo userInfo = new UserInfo();
	    
	    if(1 == modify){
	    	String id = request.getParameter("ID");
	    	String userName = request.getParameter("userName");
			String sex = request.getParameter("sex");
	    	int age = Integer.parseInt(request.getParameter("age") == null?"19":request.getParameter("age"));
	    	String telephone = request.getParameter("telephone");
	    	String QQ = request.getParameter("QQ");
	    	String email = request.getParameter("email");
	    	String local = request.getParameter("local");
	    	String userIntro = request.getParameter("userIntro");
	    	String userFace = request.getParameter("userFace");
	    	
	    	userInfo.setAge(age);
	    	userInfo.setEmail(email);
	    	userInfo.setID(id);
	    	userInfo.setLocal(local);
	    	userInfo.setQQ(QQ);
	    	userInfo.setSex(sex);
	    	userInfo.setTelephone(telephone);
	    	userInfo.setUserIntro(userIntro);
	    	userInfo.setUserName(userName);
	    	userInfo.setUserFace(userFace);
	    }
	    IUserInfoDao userInfoDao = DaoFactory.getUserInfoDao();
	    
	    boolean falg = false;
	    
	    try {
			falg = userInfoDao.doUpdate(userInfo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	    
	    if (falg) {
	    	try {
				response.getWriter().print("<script  language='javascript'>alert('修改成功');</script>");
				response.sendRedirect("UserInfo?id="+userInfo.getID());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			try {
				response.getWriter().print("<script  language='javascript'>alert('请求的用户信息有误');history.go(-1);</script>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
